package com.ht.medicaltreatment.activity.haocai

import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.viewModels
import androidx.core.view.forEach
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.bean.BuildProjectConsumableUpdate
import com.ht.medicaltreatment.http.api.BuildProjectConsumableUpdateAPI
import com.ht.medicaltreatment.viewmodel.CreateZTViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etFormName
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etGName
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etProjectCode
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etSampleName
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.etUser
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.imgAdd
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.llContent
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.scroll
import kotlinx.android.synthetic.main.activity_create_zhuanti_layout.tvCheckCount
import kotlinx.android.synthetic.main.activity_update_zhuanti_layout.btnCreate
import kotlinx.android.synthetic.main.activity_update_zhuanti_layout.btnDelete

/**
 * 修改和删除 专题
 */
class UpdateZhuanTiActivity:BaseActivity() {

    override fun getLayoutId() = R.layout.activity_update_zhuanti_layout

    private val model by viewModels<CreateZTViewModel>()
    private var code = 0
    private var id = -1

    override fun init() {
        setTitle("编辑专题")
        id = intent.getIntExtra("projectId",-1)

        //添加
        imgAdd.setOnClickListener {
            val view = getView()
            val tvCode = view.findViewById<TextView>(R.id.tvCode)
            code++
            tvCode.setText("$code")
            llContent.addView(view)
            scroll.post {
                scroll.fullScroll(View.FOCUS_DOWN)
            }
            tvCheckCount.text = "$code"
        }

        model.detail("$id"){
            etFormName.setText(it.name)
            etSampleName.setText(it.protocolNumber )
            etUser.setText(it.responsiblePerson )
            etProjectCode.setText(it.projectNumber)
            etGName.setText(it.drugName )
            code = it.projectConsumableList.size
            tvCheckCount.text = "$code"
            it.projectConsumableList.forEachIndexed { index, bean ->
                val view = getView()
                val tvCode = view.findViewById<TextView>(R.id.tvCode)
                val etName = view.findViewById<TextView>(R.id.etName)
                val etRongLiang = view.findViewById<TextView>(R.id.etRongLiang)
                val etCount = view.findViewById<TextView>(R.id.etCount)
                etName.setText(bean.name)
                etRongLiang.setText(bean.specification)
                etCount.setText("${bean.number}")
                tvCode.setText("${index+1}")
                tvCode.setTag("${bean.id}")
                llContent.addView(view)
            }
        }

        //校验专题号
        etSampleName.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus)return@setOnFocusChangeListener
            model.checkNumber("$id", "1", etSampleName.text.toString()) {
                if (!it) {
                    showToast("该专题号已存在，请修改")
                }
            }
        }

        //修改
        btnCreate.setOnClickListener {
            change()
        }

        //删除
        btnDelete.setOnClickListener {
            showProgress()
            model.delete("$id"){
                finish()
            }
        }
    }

    fun change(){
        showProgress()
        //药物名称;供试品名称
        val drugName = etGName.text.toString()
        //项目名称  专题名称
        val name = etFormName.text.toString()
        //项目编号
        val projectNumber = etProjectCode.text.toString()
        //专题号
        val protocolNumber  = etSampleName.text.toString()
        //负责人/主要研究者;专题负责人
        val responsiblePerson   = etUser.text.toString()
        val list = arrayListOf<BuildProjectConsumableUpdate>()
        llContent.forEach {
            val etName = it.findViewById<EditText>(R.id.etName)
            val etRongLiang = it.findViewById<EditText>(R.id.etRongLiang)
            val etCount = it.findViewById<EditText>(R.id.etCount)
            val tvCode = it.findViewById<TextView>(R.id.tvCode)
            val tag = tvCode.getTag()
            var id = ""
            if(tag != null){
                id = tag as String
            }
            list += BuildProjectConsumableUpdate(id,etName.text.toString(),etCount.text.toString(),etRongLiang.text.toString())
        }
        val api =  BuildProjectConsumableUpdateAPI(
            id = "$id",
            drugName = drugName,
            name = name,
            projectNumber = projectNumber,
            protocolNumber = protocolNumber,
            responsiblePerson = responsiblePerson,
            projectConsumableList = list)


        model.change(api){
            showToast("修改成功")
            finish()
        }
    }

    fun getView():View{
        val view = LayoutInflater.from(this).inflate(R.layout.item_create_zhuanti_layout,null,false)
        val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        params.topMargin = 20
        params.leftMargin = 10
        params.rightMargin = 10
        view.layoutParams = params
        return view
    }
}